package org.concepts.java.antlr.csv;

import org.antlr.runtime.ANTLRStringStream;
import org.concepts.java.antlr.utils.AntlrUtils;
import org.junit.Test;


public class TestCSVLexer {


  @Test
  public void testNormalInput() {
    processInputUsingCSVLexer("1");
    processInputUsingCSVLexer("1,2");
    processInputUsingCSVLexer("10,11,12");
  }

  @Test
  public void testWithNewline() {
    processInputUsingCSVLexer("1,2,\n3");
    processInputUsingCSVLexer("10,11\n\r\n,3,4,5");
  }

  @Test
  public void testWithSingleLineComment() {
    processInputUsingCSVLexer("1,2,3//Some comment\n");
    processInputUsingCSVLexer("10,11//Another comment\n,12");
  }

  @Test
  public void testWithMultiLineComment() {
    processInputUsingCSVLexer("1,2,3/*Some multi line \n \n comment */");
    processInputUsingCSVLexer("10,11/*First multie line comment*/,12"
        + "/*Second \n another \n multi \r\n line comment*/,13");
  }

  @Test
  public void testAutomaticRecoveryIgnoreNonIntegers() {
    processInputUsingCSVLexer("1,2\n,3,a,4");
  }

  @Test
  public void testAutomaticRecoveryInvalidSeparator() {
    processInputUsingCSVLexer("1;2");
  }
  
  /**
   * Process the input using {@link CSVLexer}
   * 
   * @param input
   *          input. Cannot be null
   */
  private void processInputUsingCSVLexer(String input) {
    System.out.println("\n\nInput [" + input + "]");
    CSVLexer csvLexer = new CSVLexer(new ANTLRStringStream(input));
    AntlrUtils.processAndPrintTokens(csvLexer);
    System.out.println("Number of integers: " + csvLexer.getIntegerCount());
  }
  
}
